<script setup lang="ts">
import { ref } from 'vue'
import { useUpload } from '@/utils/uploadFile'

const { loading, error, data, progress, run } = useUpload('/mch/tpl/uploadImg.html')

const fileList = ref([])

function customUpload(file, formData, options) {
  const { loading, error, data, progress, run } = useUpload('/mch/tpl/uploadImg.html', {
    act: 'uploadImg',
    pic_name: 'license_pic',
  }, {}, file.url)
  run()
  if (error.value) {
    options.onError(error.value, file, formData)
  }
  else {
    options.onSuccess('res', file, formData)
  }

  options.onProgress(loading, error, data, progress)

  return
  const uploadTask = uni.uploadFile({
    url: '/mch/tpl/uploadImg.html',
    header: {
      'Content-Type': 'multipart/form-data',
    },

    name: options.name,
    fileName: options.name,
    fileType: options.fileType,
    filePath: file.url,
    success(res) {
      if (res.statusCode === options.statusCode) {
        // 设置上传成功
        options.onSuccess(res, file, formData)
      }
      else {
        // 设置上传失败
        options.onError({ ...res, errMsg: res.errMsg || '' }, file, formData)
      }
    },
    fail(err) {
      // 设置上传失败
      options.onError(err, file, formData)
    },
  })
  // 设置当前文件加载的百分比
  uploadTask.onProgressUpdate((res) => {
    options.onProgress(res, file)
  })
}
</script>

<template>
  <wd-upload v-model:file-list="fileList" :limit="1" :upload-method="customUpload" />
  <!-- <button @click="run">
    上传{{ progress }} {{ error }}{{ data }}
  </button> -->
</template>

<style>
</style>
